package org.twf.aicodehelper.service;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AiCodeHepler {

    @Resource
    @Qualifier("qwenChatModel")
    private ChatModel qwenChatModel;

    /**
     * 文本对话
     *
     * @param message
     * @return
     */
    public String chat(String message) {
        UserMessage from = UserMessage.from(message);
        ChatResponse chat = qwenChatModel.chat(from);
        AiMessage aiMessage = chat.aiMessage();
        log.info("Chat Message: {}", aiMessage.toString());
        return aiMessage.text();
    }

    /**
     * 多模态能力
     * 需要 qwen3-omni-flash
     *
     * @param userMessage
     * @return
     */
    public String chatWithMessage(UserMessage userMessage) {
        ChatResponse chat = qwenChatModel.chat(userMessage);
        AiMessage aiMessage = chat.aiMessage();
        log.info("Chat Message: {}", aiMessage.toString());
        return aiMessage.text();
    }

    private static final String SYSTEM_MESSAGE = "你是编程领域的助手，可以解答编程学习等技术相关的问题，并给出建议，重点关注三个方向：\n" +
            "1. 从模糊的描述中提取清晰明确的需求并确认\n" +
            "2. 将需求转化为可执行的操作步骤并予以说明\n" +
            "3. 给出可供参考的其他实现方案\n" +
            "请用简洁易懂的语言回答，助力用户高效开发与学习。";

    /**
     * 带系统提示词的对话
     *
     * @param message
     * @return
     */
    public String chatWithSystemMessage(String message) {
        SystemMessage systemMessage = SystemMessage.from(SYSTEM_MESSAGE);
        UserMessage from = UserMessage.from(message);
        ChatResponse chat = qwenChatModel.chat(systemMessage, from);
        AiMessage aiMessage = chat.aiMessage();
        log.info("Chat Message: {}", aiMessage.toString());
        return aiMessage.text();
    }

}